package com.yiqi.lottery.feature.mine.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.OrderDetailBo;
import com.yiqi.lottery.feature.lottery.view.adapter.TableAdpter;
import com.yiqi.lottery.feature.mine.contract.DetailContract;
import com.yiqi.lottery.feature.mine.dependencies.detail.DaggerDetailComponent;
import com.yiqi.lottery.feature.mine.dependencies.detail.DetailPresenterModule;

import java.text.DecimalFormat;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderInfoActivity extends BaseActivity implements DetailContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @Inject
    DetailContract.Presenter mPresenter;
    @BindView(R.id.tv_lottery_order_detail_name)
    TextView mTvLotteryOrderDetailName;
    @BindView(R.id.tv_lottery_order_detail_no)
    TextView mTvLotteryOrderDetailNo;
    @BindView(R.id.tv_lottery_order_detail_amount)
    TextView mTvLotteryOrderDetailAmount;
    @BindView(R.id.tv_lottery_order_detail_info)
    TextView mTvLotteryOrderDetailInfo;
    @BindView(R.id.tv_lottery_order_detail_status)
    TextView mTvLotteryOrderDetailStatus;
    @BindView(R.id.tv_lottery_order_detail_date)
    TextView mTvLotteryOrderDetailDate;
    @BindView(R.id.rv_table)
    RecyclerView mRvTable;
    @BindView(R.id.view_order)
    RelativeLayout mViewOrder;

    @BindView(R.id.tv_lottery_order_detail_beishu)
    TextView mTvLotteryOrderDetailBeishu;
    @BindView(R.id.tv_lottery_order_detail_is_zhuijia)
    TextView mTvLotteryOrderDetailIsZhuijia;

    @BindView(R.id.view_da_lotto)
    LinearLayout mViewDaLotto;
    @BindView(R.id.tv_top)
    TextView mTvTop;
    @BindView(R.id.tv_reward_tip)
    TextView mTvRewardTip;
    @BindView(R.id.view_not_lottery)
    LinearLayout mViewNotLottery;
    @BindView(R.id.view_not_winning)
    LinearLayout mViewNotWinning;
    @BindView(R.id.tv_show_winning_pize)
    TextView mTvShowWinningPize;
    @BindView(R.id.view_had_winning)
    RelativeLayout mViewHadWinning;
    @BindView(R.id.tv_lottery_order_detail_fadanren)
    TextView tvLotteryOrderDetailFadanren;

    private int mType;
    private TableAdpter mAdapter;

    LinearLayout contentView, contentView2;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order_info;
    }

    @Override
    protected void initInjector() {

        DaggerDetailComponent.builder().detailPresenterModule(new DetailPresenterModule(this)).build().inject(this);

    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "订单详情", true, true);
        LayoutInflater mInflater = LayoutInflater.from(this);
        contentView = (LinearLayout) mInflater.inflate(R.layout.item_lottery_hall_title, null);
        contentView2 = (LinearLayout) mInflater.inflate(R.layout.item_lottery_hall_title2, null);
    }

    @Override
    protected void initData() {
        initAdapter();
        Integer id = this.getIntent().getIntExtra("id", 0);
        mType = this.getIntent().getIntExtra("type", 0);
        mPresenter.getOrderDetail(id, 1);


    }


    @Override
    public void showOrderDetailSuccess(OrderDetailBo msg) {

        if (msg == null) return;
        String stauts = "";
        if (msg.getOrder_status() == 1) {
            stauts = "待开奖";
            mTvTop.setText("奖金范围：" + msg.getMin_reward() + "-" + msg.getMax_reward() + "");
            mTvRewardTip.setText("奖金范围仅供参考");
            mViewNotLottery.setVisibility(View.VISIBLE);
        } else if (msg.getOrder_status() == 2) {
            stauts = "未中奖";
            mViewNotWinning.setVisibility(View.VISIBLE);
        } else if (msg.getOrder_status() == 3) {
            stauts = "已中奖";
            mTvShowWinningPize.setText(getTwoDecimal(msg.getReward()) + "元");
            mViewHadWinning.setVisibility(View.VISIBLE);
        }
        if (msg.getBet_type() == 1) {
            mRvTable.setVisibility(View.GONE);
            mViewOrder.setVisibility(View.GONE);
            mViewDaLotto.setVisibility(View.VISIBLE);
//                if (mAdapter != null) mAdapter.getHeaderLayout().setVisibility(View.GONE);
            mTvLotteryOrderDetailIsZhuijia.setText(Html.fromHtml("是否追加：<font color=\"#383838\">" + (msg.getIs_follow() == 3 ? "是" : "否") + "</font>"));
            mTvLotteryOrderDetailBeishu.setText(Html.fromHtml("倍数：<font color=\"#383838\">" + msg.getBet_info().getMultiple() + "</font>"));

        } else {
            mViewDaLotto.setVisibility(View.GONE);
        }
        mTvLotteryOrderDetailName.setText(msg.getBet_label());
        mTvLotteryOrderDetailNo.setText(Html.fromHtml("订单号：<font color=\"#383838\">" + msg.getBet_info().getIssue_id() + "</font>"));
        mTvLotteryOrderDetailAmount.setText(Html.fromHtml("投注金额：<font color=\"#383838\">" + msg.getOrder_amount() + "元</font>"));
        mTvLotteryOrderDetailInfo.setText(Html.fromHtml("投注信息：<font color=\"#383838\">" + (msg.getBet_type() == 1 ? msg.getBet_info().getBet_code() : msg.getPass_type()) + "</font>"));
        mTvLotteryOrderDetailStatus.setText(Html.fromHtml(msg.getOrder_status() == 3 ? "<font color=\"#6B6B6B\">状态 :</font> <font color=\"#D83200\">" + stauts + "</font>" : "<font color=\"#383838\">订单状态 :</font> " + stauts + ""));
        mTvLotteryOrderDetailDate.setText(Html.fromHtml("支付时间：<font color=\"#383838\">" + DateTool.TimeStamp2Date(msg.getCreated_at()) + "</font>"));

        if (msg.getBet_plan() != null && msg.getBet_plan().size() >= 0) {
            mRvTable.setVisibility(View.VISIBLE);
            mAdapter.setNewData(msg.getBet_plan());
            mAdapter.setmType(msg.getBet_type());

        } else {
            mRvTable.setVisibility(View.GONE);
        }

        if (msg.getBet_type() == 1) {
//            mAdapter.addHeaderView(contentView2);
        } else if (msg.getBet_type() == 5) {
            mAdapter.addHeaderView(contentView2);
        } else {
            mAdapter.addHeaderView(contentView);

        }

        if (mType == 2) {
tvLotteryOrderDetailFadanren.setVisibility(View.VISIBLE);
            tvLotteryOrderDetailFadanren.setText(Html.fromHtml("发单人：<font color=\"#383838\">" + msg.getUser_info().getUsername() + "</font>"));
        }else {
            tvLotteryOrderDetailFadanren.setVisibility(View.GONE);
        }

    }

    private void initAdapter() {

        mAdapter = new TableAdpter();
        mRvTable.setLayoutManager(new LinearLayoutManager(this));
        mRvTable.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight((int) 0.2));
        mAdapter.bindToRecyclerView(mRvTable);

    }

    @Override
    public void showOrderDetailError(String msg) {
        T.showShort("" + msg);
    }

    /**
     * 将数据保留两位小数
     */
    private double getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.00");
        String yearString = dFormat.format(num);
        Double temp = Double.valueOf(yearString);
        return temp;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
